<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">

    <span th:replace="common_page::common"></span>
</head>
<body>

<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" id="categorySearchForm" action="">
                    <input type="hidden" id="shopId" name="shopId" th:value="${shopId}" />
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nameLike" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>
            </div>
        </script>

        <table class="layui-hide" id="shopCategoryTreeTable" lay-filter="shopCategoryTreeTableFilter"></table>

        <!-- 操作列 -->
        <script type="text/html" id="shopCategoryTreeTableToolbar">
            <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
        </script>

    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script th:src="@{/js/common/selectApp.js}" charset="utf-8"></script>

<script>


    layui.config({
        base: basePath+'/lib/layui_ext/treeTable/' //配置 layui 第三方扩展组件存放的基础目录
    }).extend({
        treeTable: 'treeTable' //定义该组件模块名
    }).use(['form', 'table', 'treeTable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var treetable = layui.treeTable;
        var loadding;
        var treetableHandler;

        $.fn.serializeJson=function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        };

        function getFormParam(row)
        {
            var formParam = $("#categorySearchForm").serializeJson();
            if(row!=null) {
                formParam.parentId = row.id;
            }else{
                formParam.parentId=-1;
            }
            return formParam;
        }

        function loadTreeTable()
        {
            loadding = layer.load();
            treetableHandler = treetable.render({
                elem: '#shopCategoryTreeTable',
                tree: {
                    iconIndex: 2
                },
                toolbar: '#toolbarDemo',
                page: false,
                cols: [
                    [
                        {type: 'numbers'},
                        {type: 'checkbox'},
                        {field: 'name', width: 200, title: '名称'},
                        {field: 'icon', width: 100,  title: '图标'},
                        {field: 'categorySort', title: '排序'},
                        {field: 'createDate',  title: '创建时间'},
                        {field: 'updateDate',  title: '修改时间'},
                        {templet: '#shopCategoryTreeTableToolbar', width: 450, align: 'center', title: '操作'}
                    ]
                ],
                reqData: function (selectRow, callback) {
                    $.ajax({
                        url:"[[@{/}]]seller/shop/category/tree/table/by/pid",
                        type:'GET',
                        data:getFormParam(selectRow),
                        success:function(res){
                            callback(res.data);
                        },
                        complete:function(data)
                        {
                            layer.closeAll('loading');
                            layer.close(loadding);
                        }
                    });
                }
            });

        }

        loadTreeTable();

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            loadTreeTable();

            return false;
        });

        /**
         * toolbar监听事件
         */
        treetable.on('toolbar(shopCategoryTreeTable)', function (obj) {

            if (obj.event === 'add') {  // 监听添加操作
                var shopId = $("#shopId").val();
                var index = layer.open({
                    title: '添加类别',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: "[[@{/}]]seller/shop/category/addPage/"+shopId+"/-1"
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        treetable.on('checkbox(shopCategoryTreeTable)', function (obj) {
            console.log(obj)
        });

        treetable.on('tool(shopCategoryTreeTable)', function (obj) {
            var shopId = $("#shopId").val();
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑分类',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: '[[@{/}]]seller/shop/category/editPage/'+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (obj.event === 'addChildCategory') {
                var index = layer.open({
                    title: '添加子分类',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: "[[@{/}]]seller/shop/category/addPage/"+shopId+"/"+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该分类?', function (index) {
                    $.ajax({
                        url:"[[@{/}]]seller/shop/category/delete/"+data.id+"/"+shopId,
                        contentType: "application/json; charset=utf-8",
                        type:'DELETE',
                        success:function(data){
                            layer.msg(data.msg);

                            //执行搜索重载
                            loadTreeTable();

                        },
                        complete:function(data)
                        {
                            layer.close(index);
                        }
                    });
                });
            }else if (obj.event === 'moveUp') {
                $.ajax({
                    url:"[[@{/}]]seller/shop/category/move/up",
                    contentType: "application/json; charset=utf-8",
                    type:'POST',
                    data:JSON.stringify({shopId:data.shopId,id:data.id,parentId:data.parentId}),
                    success:function(data){
                        layer.msg(data.msg);

                        //执行搜索重载
                        loadTreeTable();

                    },
                    complete:function(data)
                    {
                        layer.close(index);
                    }
                });
            }else if (obj.event === 'moveDown') {
                $.ajax({
                    url:"[[@{/}]]seller/shop/category/move/down",
                    contentType: "application/json; charset=utf-8",
                    type:'POST',
                    data:JSON.stringify({shopId:data.shopId,id:data.id,parentId:data.parentId}),
                    success:function(data){
                        layer.msg(data.msg);

                        //执行搜索重载
                        loadTreeTable();

                    },
                    complete:function(data)
                    {
                        layer.close(index);
                    }
                });
            }else if (obj.event === 'moveTop') {
                $.ajax({
                    url:"[[@{/}]]seller/shop/category/move/top",
                    contentType: "application/json; charset=utf-8",
                    type:'POST',
                    data:JSON.stringify({shopId:data.shopId,id:data.id,parentId:data.parentId}),
                    success:function(data){
                        layer.msg(data.msg);

                        //执行搜索重载
                        loadTreeTable();

                    },
                    complete:function(data)
                    {
                        layer.close(index);
                    }
                });
            }else if (obj.event === 'moveBottom') {
                $.ajax({
                    url:"[[@{/}]]seller/shop/category/move/bottom",
                    contentType: "application/json; charset=utf-8",
                    type:'POST',
                    data:JSON.stringify({shopId:data.shopId,id:data.id,parentId:data.parentId}),
                    success:function(data){
                        layer.msg(data.msg);

                        //执行搜索重载
                        loadTreeTable();

                    },
                    complete:function(data)
                    {
                        layer.close(index);
                    }
                });
            }
        });

    });
</script>

</body>
</html>